package tc.de.krawallmann.gravoblocks.score;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * The class Highscores manages all scores.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class Highscores extends TreeSet<Score> {

	/** The constant serialVersionUID. */
	private static final long serialVersionUID = 80788309807722180L;

	/**
	 * Adds the score.
	 * 
	 * @param score
	 *            the score to be added
	 * @return true, if there was no equal score!
	 */
	public boolean addScore(final Score score) {
		return add(score);
	}

	/**
	 * Checks if there is an "equal" Score.
	 * 
	 * @param gamemode
	 * @param score
	 *            the Score to be checked
	 * @return true, if there is such a Score
	 */
	public boolean contains(final String gamemode, final int score) {
		boolean ret = false;
		for (final Score s : this) {
			if ((s.getPoints() == score) && !s.isCheat() && gamemode.equals(s.getGamemode())) {
				ret = true;
				break;
			}
		}
		return ret;
	}

	/**
	 * Gets the Score at the given index.
	 * 
	 * @param index
	 *            the index
	 * @return the Score at the given index
	 */
	public Score getScore(final int index) {
		if (index > size()) {
			throw new IllegalArgumentException(index + " exceeds the score amount of " + size());
		}
		final Iterator<Score> iterator = iterator();
		Score ret = iterator.next();
		for (int i = 1; i < index; i++) {
			ret = iterator.next();
		}
		return ret;
	}

}
